package com.testmanager.testmananger.foundation.config;

import com.testmanager.testmananger.foundation.util.FlakeIdGenerator;
import com.testmanager.testmananger.foundation.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.print.attribute.standard.Media;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

@Configuration
@Slf4j
public class AuthenticationInterceptor  implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        log.info("AuthenticationInterceptor");
        log.info("path:{}",request.getPathInfo());
        String token = request.getHeader("token");
        boolean verity = JWTUtil.verity(token);
       if (!verity){
           log.info("false");
           response.setStatus(401);
           return false;

       }
       log.info("user:{}",JWTUtil.getName(token));
//        HandlerMethod handlerMethod = (HandlerMethod)handler;
//
//        Method method = handlerMethod.getMethod();


        return true;
    }


    @Bean
    public FlakeIdGenerator getFlake(){
        return  new FlakeIdGenerator(01);
    }


}
